package com.quan.commons.utils;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

/**
 * 分页工具类
 */
public class PageBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private long total = 0L;           //总记录数
    private int pageNum = 1;           //当前页数
    private int pageSize = 15;         //每页记录数
    private long totalPage = 1;        //总页数
    private String sortName;           //排序名称
    private String sortOrder;          //排序类型（DESC：降序，ASC：降序）
    private String orderBy;            //排序字符串：ID DESC
    private String queryString;        //当前页所有参数

    public static void main(String[] args) {
        PageBean p = new PageBean() ;
        System.out.println("总记录数："+p.getTotal());
        System.out.println("当前页："+p.getPageNum());
        System.out.println("每页记录数："+p.getPageSize());
        System.out.println("总页数："+p.getTotalPage());
    }

    public PageBean() {
    }

    public PageBean(HttpServletRequest request) {
        init(request);
    }

    public PageBean(long total) {
        this.total = total;
    }

    public PageBean(long total, int pageNum, int pageSize) {
        this.total = total;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public PageBean(long total, int pageNum, int pageSize, String orderBy) {
        this.total = total;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.orderBy = orderBy;
    }

    public PageBean(long total, int pageNum, int pageSize, String sortName, String sortOrder) {
        this.total = total;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.sortName = sortName;
        this.sortOrder = sortOrder;
    }
    
    public PageBean(int pageNum, int pageSize) {
		super();
		this.pageNum = pageNum;
		this.pageSize = pageSize;
	}

	public PageBean(int pageNum, int pageSize, String orderBy) {
		super();
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.orderBy = orderBy;
	}

	public PageBean(String orderBy) {
		this.orderBy = orderBy;
	}

	private void init(HttpServletRequest request) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        String sortName = request.getParameter("sortName");
        String sortOrder = request.getParameter("sortOrder");
        String orderBy = request.getParameter("orderBy");
        this.setQueryString(request.getQueryString());
        
        if(CommonUtils.isNotEmpty(pageNum)) {
            setPageNum(Integer.parseInt(pageNum));
        }
        if(CommonUtils.isNotEmpty(pageSize)) {
            setPageSize(Integer.parseInt(pageSize));
        }
        if(CommonUtils.isNotEmpty(sortName)) {
            setSortName(sortName);
        }
        if(CommonUtils.isNotEmpty(sortOrder)) {
            setSortOrder(sortOrder);
        }
        if(CommonUtils.isNotEmpty(orderBy)) {
            setOrderBy(orderBy);
        }
        if(CommonUtils.isNotEmpty(sortName) && CommonUtils.isNotEmpty(sortOrder)) {
            setOrderBy(this.sortName + " " + this.sortOrder);
        }
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }
    
    public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotalPage() {
        if(totalPage == 1) {
            return this.totalPage = (total % pageSize) == 0 ? (total / pageSize) : ((total / pageSize) + 1) ;
        } else {
            return this.totalPage;
        }
    }

    public void setTotalPage(long totalPage) {
        this.totalPage = totalPage;
    }

    public String getSortName() {
        return sortName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getQueryString() {
        return queryString;
    }

    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }
}
